package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Question;
import cn.wolfcode.trip.base.mapper.QuestionMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IQuestionService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class QuestionServiceImpl implements IQuestionService {
    @Autowired
    private QuestionMapper questionMapper;


    @Override
    public void saveOrUpdate(Question question) {
        if (question.getId() != null) {
            // 更新
            questionMapper.updateByPrimaryKey(question);
        } else {
            //保存当前用户为提问者
            question.setQuestionUser(UserContext.getCurrentUser());
            //保存当前时间为问题的提问时间
            question.setQuestionTime(new Date());
            // 保存
            questionMapper.insert(question);
        }
    }


    @Override
    public PageInfo<Question> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Question> list = questionMapper.selectAll();
        return new PageInfo<>(list);
    }

    @Override
    public Question getById(Long id) {
        //每当进入问题详情页的时候,问题的浏览数++
        questionMapper.updateVisitCountByPrimaryKey(id);
        return questionMapper.selectByPrimaryKey(id);
    }


}
